package com.syqc.videos.real

import android.view.View
import android.widget.SeekBar
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels 
import com.syqc.interfaces.SimpleSeekBarChangeListener
import com.syqc.base.BaseRealFragment
import com.syqc.monitor.databinding.FragControllerBinding

/**
 *@author  hfjs
 *@time   2024/12/31 10:29
 *@desc
 */
class ControllerFragment(private val type: Int) : BaseRealFragment<FragControllerBinding>(type,FragControllerBinding::inflate) {
    private val mVM by viewModels<ControllerVM>()

    override fun hide() {

    }

    override fun initView(view: View) {
        showControlView()
    }

    override fun initData() {
        mVM.actionLive.observe(this) {
            val action = mVM.getAction()
            binding.ivLight.isSelected = action.light == 1
            binding.ivWiper.isSelected = action.wiper == 1
            binding.ivWiperO.isSelected = action.wiper == 1
        }

    }

    override fun initEvent() {
        binding.vUp.setOnClickListener { mVM.control(1) }
        binding.vLeft.setOnClickListener { mVM.control(3) }
        binding.vCenter.setOnClickListener { mVM.control(0) }
        binding.vRight.setOnClickListener { mVM.control(4) }
        binding.vDown.setOnClickListener { mVM.control(2) }

        binding.ivFocalUp.setOnClickListener {
            mVM.changeAction(1, 0)
        }
        binding.ivFocalDown.setOnClickListener {
            mVM.changeAction(1, 1)
        }
        binding.ivApertureUp.setOnClickListener {
            mVM.changeAction(2, 0)
        }
        binding.ivApertureDown.setOnClickListener {
            mVM.changeAction(2, 1)
        }
        binding.ivZoomUp.setOnClickListener {
            mVM.changeAction(5, 0)
        }
        binding.ivZoomDown.setOnClickListener {
            mVM.changeAction(5, 1)
        }
        binding.ivLight.setOnClickListener {
            val light = mVM.getAction().light
            if (light == 0) {
                mVM.changeAction(4, 1)
            } else {
                mVM.changeAction(4, 0)
            }
        }
        binding.ivWiperO.setOnClickListener {
            val wiper = mVM.getAction().wiper
            if (wiper == 0) {
                mVM.changeAction(3, 1)
            } else {
                mVM.changeAction(3, 0)
            }
        }
        binding.ivWiper.setOnClickListener {
            val wiper = mVM.getAction().wiper
            if (wiper == 0) {
                mVM.changeAction(3, 1)
            } else {
                mVM.changeAction(3, 0)
            }
        }

        binding.sbSpeed.setOnSeekBarChangeListener(object : SimpleSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                super.onProgressChanged(seekBar, progress, fromUser)
                mVM.speed = seekBar?.progress ?: 0
                binding.tvSpeedNum.text = mVM.speed.toString()
            }
        })
    }

    fun setCarId(carId: String, is28181: Boolean, ctrlChannel: String) {
        mVM.carId = carId
        mVM.is28181 = is28181
        mVM.ctrlChannel = ctrlChannel
        showControlView()
    }

    private fun showControlView() {
        val action = mVM.getAction()
        binding.ivLight.isSelected = action.light == 1
        binding.ivWiper.isSelected = action.wiper == 1
        binding.ivWiperO.isSelected = action.wiper == 1

        binding.llAperture.isInvisible = mVM.is28181
        binding.llFocal.isGone = mVM.is28181
        binding.llWiper.isInvisible = mVM.is28181
        binding.llWiperO.isVisible = mVM.is28181
    }
}